<template>
  <div class="sku">
    <!-- 搜索框 -->
    <div v-if="search_visible" id="search" class="search">
      <el-form ref="form" :model="searchData">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item>
              <el-input
                id="company_name"
                v-model="searchData.product_title"
                placeholder="请输入商品名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-select
                style="display: block"
                v-model="searchData.order_status"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option key="0" label="开启" value="1"></el-option>
                <el-option key="1" label="锁住" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <search-tool
                :search_data.sync="searchData"
                @refesh_list="getProductSkuList"
              ></search-tool>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 工具栏 -->
    <div class="sku_header_left">
      <div>
        <el-button
          type="primary"
          size="small"
          class="el-icon-plus"
          @click="addShops"
        >
          新增SKU
        </el-button>
        <el-button
          size="small"
          @click="downloadExcel"
          class="el-icon-download"
          type="primary"
        >
          下载模板
        </el-button>
        <el-upload
          style="display: inline-block"
          action
          accept=".xlsx, .xls"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="handleExcel"
        >
          <el-button size="small" class="el-icon-upload2" type="primary">
            上传EXCEL
          </el-button>
        </el-upload>
      </div>
      <div>
        <TableTool
          :search_visible.sync="search_visible"
          @refesh_list="getProductSkuList"
        ></TableTool>
      </div>
    </div>
    <!-- 数据列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{
        background: '#f5f7fa',
        color: '#14142B',
        height: '28px',
        padding: '5px 0',
      }"
      :cell-style="{
        color: '#606266',
        fontSize: '13px',
        padding: '7px 0',
      }"
      :height="
        search_visible
          ? $store.state.user.clientHeight - 290
          : $store.state.user.clientHeight - 250
      "
      v-loading="loading"
      row-key="keyId"
    >
      <template slot="empty">
        <img
          class="empty-img"
          src="@/assets/imgs/zanwushuju.svg"
          style="width: 64px; height: 50px"
          alt=""
        />
        <div class="noCart">暂无数据</div>
      </template>
      <el-table-column align="center" type="selection" width="80">
      </el-table-column>
      <!--      <el-table-column align="" label="#" type="index" width="80">-->
      <!--      </el-table-column>-->
      <el-table-column
        align=""
        property="sku_No"
        label="商品SKU"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column align="" label="商品名称" prop="product_title">
        <template slot-scope="scope">
          <div
            v-if="scope.row.product_title.zh"
            class="beyond"
            style="word-break: break-all"
          >
            <span class="labels"> 中文品名：</span
            >{{ scope.row.product_title.zh }}
          </div>
          <div
            v-if="scope.row.product_title.en"
            class="beyond"
            style="word-break: break-all"
          >
            <span class="labels"> 英文品名：</span
            >{{ scope.row.product_title.en }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="" label="商品类型" prop="product_category">
        <template slot-scope="scope">
          <div
            v-if="scope.row.product_category.zh"
            style="word-break: break-all"
            class="beyond"
          >
            <span class="labels"> 中文：</span
            >{{ scope.row.product_category.zh }}
          </div>
          <div
            v-if="scope.row.product_category.en"
            style="word-break: break-all"
            class="beyond"
          >
            <span class="labels"> 英文：</span
            >{{ scope.row.product_category.en }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="" label="商品描述" prop="product_describe">
        <template slot-scope="scope">
          <div
            v-if="scope.row.product_describe.zh"
            style="word-break: break-all"
            class="beyond"
          >
            <span class="labels">中文描述：</span
            >{{ scope.row.product_describe.zh }}
          </div>
          <div
            v-if="scope.row.product_describe.en"
            style="word-break: break-all"
            class="beyond"
          >
            <span class="labels">英文描述：</span
            >{{ scope.row.product_describe.en }}
          </div>
        </template>
      </el-table-column>

      <el-table-column align="" label="商品属性" prop="specifications">
        <template slot-scope="scope">
          <el-checkbox
            v-if="scope.row.specifications"
            v-model="scope.row.specifications.charged"
            class="checkbtn"
            ><span class="labels">是否带电?</span>
          </el-checkbox>
          <el-checkbox
            v-if="scope.row.specifications"
            v-model="scope.row.specifications.liquid"
            class="checkbtn"
            ><span class="labels">是否液体?</span>
          </el-checkbox>
          <div v-if="scope.row.specifications" class="labels">
            <span>颜色：</span>{{ scope.row.specifications.color }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align=""
        property="hs_code"
        label="hscode"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        align=""
        property="univalent"
        label="单价"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        align=""
        property="amount"
        label="库存"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        align=""
        property="create_time"
        label="创建时间"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.create_time }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="text" size="small"
            ><i class="el-icon-edit"></i> 编辑
          </el-button>
          <el-button @click="delClick(scope.row)" type="text" size="small"
            ><i class="el-icon-delete"></i> 删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- Sku模态框 -->
    <SkuModal
      @submit="submitClick"
      @closeForm="closeDialog"
      :dialogVisible.sync="dialogVisible"
      :editData="editData"
      :dialogTitle="dialogTitle"
    ></SkuModal>
  </div>
</template>

<script>
import SkuModal from "./components/SkuModal.vue";
import {
  addProductSku,
  getProductSkuList,
  deleteProductSku,
  updateProductSku,
  skuForm,
  getUpdateInfo,
} from "@/api/sku";
import globalMethod from "@/utils/ExcelToJson";

export default {
  components: {
    SkuModal,
  },
  data() {
    return {
      //---------------------------------------
      searchData: {},
      search_visible: true,
      //---------------------------------------
      loading: false,
      formInline: {
        page: 1,
        limit: 20,
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
      tableData: [],
      dialogVisible: false,
      dialogTitle: "",
      editData: {},
    };
  },
  created() {
    this.getProductSkuList(this.formInline);
  },
  methods: {
    //新增（编辑）提交
    submitClick(val) {
      if (val.product_sku_id) {
        this.updateProductSku(val);
      } else {
        this.addProductSku(val);
      }
    },
    //新增
    addShops() {
      this.editData = {
        hs_code: "",
        product: [
          {
            country_abb: "",
            product_title: "",
            product_describe: "",
            product_category: "",
          },
        ],
        sku_No: "",
        univalent: "",
        weight: "",
        volume: {
          L: "",
          W: "",
          H: "",
        },
        specifications: {
          charged: false,
          liquid: false,
          color: "",
        },
      };
      this.dialogTitle = "新增";
      this.dialogVisible = true;
    },
    //新增保存产品
    async addProductSku(option) {
      const res = await addProductSku(option);
      if (res.data.status === 200) {
        this.dialogVisible = false;
        this.getProductSkuList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //编辑保存产品
    async updateProductSku(val) {
      const res = await updateProductSku(val);
      if (res.data.status === 200) {
        this.dialogVisible = false;
        this.getProductSkuList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //获取列表
    async getProductSkuList(val) {
      this.loading = true;
      try {
        const res = await getProductSkuList(val);
        if (res.data.status === 200) {
          this.tableData = res.data.data.data;
          //给分页器赋值
          this.pageparm.currentPage = res.data.data.current_page;
          this.pageparm.total = res.data.data.total;
          this.pageparm.pageSize = res.data.data.per_page;
        } else {
          this.$message.error(res.data.msg);
        }
        this.loading = false;
      } catch (error) {
        this.loading = false;
        this.$message.error("系统错误");
      }
    },
    //编辑sku弹窗
    async editClick(row) {
      const res = await getUpdateInfo({ product_sku_id: row.product_sku_id });
      this.editData = res.data.data;
      this.dialogTitle = "编辑";
      this.dialogVisible = true;
    },
    //删除商品sku信息
    async delClick(row) {
      this.$confirm("确定操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteProductSku({
            product_sku_id: row.product_sku_id,
          });
          if (res.data.status === 200) {
            this.getProductSkuList(this.formInline);
            this.$message({
              type: "success",
              message: res.data.msg,
            });
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        })
        .catch(() => {});
    },
    //------------------------------弹窗、分页器--------------------------------------------------------------------------------------
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
    },
    //分页插件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getProductSkuList(this.formInline);
    },
    handleExcel(file) {
      let ExcelJsonData = globalMethod.ExcelToJson(file);
      ExcelJsonData.then(async (res) => {
        let validData = res.splice(1);
        const option = await skuForm({ skuValue: validData });
        console.log(option);
      });
    },
    //下载模板
    downloadExcel() {},
  },
};
</script>
<style lang="scss" scoped>
.checkbtn {
  ::v-deep .el-checkbox__inner {
    border-radius: 50%;
  }
}

.beyond {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 150px;
}

.el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  text-align: center;
}

::v-deep .el-input__inner {
  height: 32px;
  line-height: 32px;
}

.sku {
  padding: 0 10px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.labels {
  font-size: 12px;
  color: #a9a9a9;

  &:hover {
    color: #000;
  }
}

.childTable {
  padding: 10px 10px;
}

.skuCalss {
  text-align: center;
  line-height: 52px;
  background-color: skyblue;
  color: #fff;
  font-weight: 400;
}

.el-icon-download {
  margin: 0 8px;
}

.sku_header_left {
  display: flex;
  justify-content: space-between;
  margin: 15px 0;
}
</style>
